package org.shipp.activity;

import java.util.LinkedList;

import org.shipp.adapter.MyListAdapter;
import org.shipp.model.Item;
import org.shipp.util.MenuEventController;
import org.shipp.util.OnSwipeTouchListener;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.Display;
import android.view.KeyEvent;
import android.view.View;
import android.view.WindowManager;
import android.view.inputmethod.InputMethodManager;
import android.webkit.WebView;
import android.widget.EditText;
import android.widget.ExpandableListView;
import android.widget.ImageButton;
import android.widget.RelativeLayout;

/**
 * 
 * @author Efren
 *
 */

public class PrincipalActivity extends Activity {

	private RelativeLayout layout;
	public static boolean open = false;
	private final Context context = this;
	private ExpandableListView listMenu;
	private RelativeLayout topoTitulo;

	private Display display;
	private EditText search;
	private ImageButton btReset;
	private MyListAdapter listAdapter;
	private WebView web;

	public static final long TEMPO_PADRAO = 500;
	public static final float INDICE_PORCENTAGEM_TELA_PADRAO = 0.8f;
	public static final int RECUO_PADRAO = -3;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.principal);
		display = ((WindowManager) getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
		this.listMenu = (ExpandableListView) findViewById(R.id.menu);
		this.layout = (RelativeLayout) findViewById(R.id.layoutToMove);
		this.topoTitulo = (RelativeLayout) findViewById(R.id.actionBar);
		this.web = (WebView) findViewById(R.id.web);

		if (savedInstanceState == null) {
			web.loadUrl("http://www.google.com.br");
		}
		search = (EditText) findViewById(R.id.search);
		btReset = (ImageButton) findViewById(R.id.bt_reset);
		search.addTextChangedListener(new TextWatcher() {
			@Override
			public void onTextChanged(CharSequence s, int start, int before, int count) {
			}

			@Override
			public void beforeTextChanged(CharSequence s, int start, int count, int after) {
			}

			@Override
			public void afterTextChanged(Editable s) {
				String query = s.toString();
				listAdapter.filterData(query);
				if (query != null && !"".equalsIgnoreCase(query)) {
					expandAll();
				} else {
					expandAFirst();
				}
			}
		});

		search.setOnKeyListener(new View.OnKeyListener() {

			@Override
			public boolean onKey(View v, int keyCode, KeyEvent event) {
				if (keyCode == KeyEvent.KEYCODE_ENTER) {
					InputMethodManager inputManager = (InputMethodManager) getSystemService(INPUT_METHOD_SERVICE);
					inputManager.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
				}
				return false;
			}
		});

		btReset.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				search.setText("");
				listAdapter.filterData("");
				expandAFirst();
			}
		});

		// search final

		displayList();

		this.layout.setOnTouchListener(new OnSwipeTouchListener(this, layout) {

			public void onSwipeRight(float ondeEsta, long tempo) {
				if (!open) {
					open = true;
					MenuEventController.open(context, layout, topoTitulo, ondeEsta, tempo);
					MenuEventController.closeKeyboard(context, getCurrentFocus());
				}
			}

			public void onSwipeLeft(float ondeEsta, long tempo) {
				if (open) {
					open = false;
					MenuEventController.close(context, layout, topoTitulo, ondeEsta, tempo);
					MenuEventController.closeKeyboard(context, getCurrentFocus());
				}
			}
		});
		RelativeLayout.LayoutParams lp = (RelativeLayout.LayoutParams) search.getLayoutParams();
		lp.width = (int) (display.getWidth() * PrincipalActivity.INDICE_PORCENTAGEM_TELA_PADRAO);
		search.setLayoutParams(lp);

		RelativeLayout.LayoutParams lpLay = (RelativeLayout.LayoutParams) layout.getLayoutParams();
		lpLay.width = display.getWidth() + 3;
		layout.setLayoutParams(lpLay);

	}

	@Override
	public void finish() {
		if (PrincipalActivity.open) {
			MenuEventController.close(context, layout, topoTitulo, (float) (display.getWidth() * PrincipalActivity.INDICE_PORCENTAGEM_TELA_PADRAO), PrincipalActivity.TEMPO_PADRAO);
			PrincipalActivity.open = false;
		} else {
			super.finish();
		}
	}

	public void openCloseMenu(View view) {
		if (!PrincipalActivity.open) {
			PrincipalActivity.open = true;
			MenuEventController.open(context, layout, topoTitulo, 0f, PrincipalActivity.TEMPO_PADRAO);
			MenuEventController.closeKeyboard(this.context, view);
		} else {
			PrincipalActivity.open = false;
			MenuEventController.close(context, layout, topoTitulo, (float) (display.getWidth() * PrincipalActivity.INDICE_PORCENTAGEM_TELA_PADRAO), PrincipalActivity.TEMPO_PADRAO);
			MenuEventController.closeKeyboard(this.context, view);
		}
	}

	private void expandAll() {
		int count = listAdapter.getGroupCount();
		for (int i = 0; i < count; i++) {
			listMenu.expandGroup(i);
		}
	}

	private void expandAFirst() {
		int count = listAdapter.getGroupCount();
		for (int i = 0; i < count; i++) {
			if (i == 0) {
				listMenu.expandGroup(i);
			} else {
				listMenu.collapseGroup(i);
			}
		}
		MenuEventController.closeKeyboard(this.context, search);
	}

	// method to expand all groups
	private void displayList() {

		// display the list
		LinkedList<Item> items = MenuEventController.getMenuDefault(this);

		// get reference to the ExpandableListView
		listMenu = (ExpandableListView) findViewById(R.id.menu);
		// create the adapter by passing your ArrayList data
		listAdapter = new MyListAdapter(PrincipalActivity.this, items);
		// attach the adapter to the list
		listMenu.setAdapter(listAdapter);
		expandAFirst();
		listMenu.setOnChildClickListener(new ExpandableListView.OnChildClickListener() {

			@Override
			public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id) {
				Item i = (Item) listAdapter.getChild(groupPosition, childPosition);
				openCloseMenu(v);
				return false;
			}
		});
	}

}